<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 基础配置
 */
namespace app\home\model;

use app\home\model\Model;
use Mscore\Core\Util\Util;
// use ms\core\Page;

class Basics extends Model{

    private $basics = 'web_basics';
    private $online  = 'user_online'; //在线表
    private $custer  = 'sys_custer'; //客服表
    private $sysbasics  = 'sys_basics'; //客服表

    /**
     * 基础信息
     * @param array $basics
     * @param array $where
     */
    public function getconfig($where='')
    {
        $basics = Model::name($this->basics)
            ->field('*')
            ->where($where)
            ->find();
        return $basics;
    }
    
     /**
     * 基础信息
     * @param array $basics
     * @param array $where
     */
    public function gettoken($tokens='')
    {
        switch (true) {
            case !empty($tokens):
                $token = Util::verifyJwtToken($tokens);
                if(!isset($token['sub']) || empty($token['sub'])){
                    return 1;
                }
                $token = $token['sub'];
                $time=time();
                $info = Model::name($this->online)
                        ->where('tkey = "'.$token["tkey"].'" and expire_time > '.$time)
                        ->field('expire_time,token')
                        ->find();
                if(!empty($info)){
                    setcookie("token", $tokens,  $info['expire_time'],'/');
                    return 2;
                }else{
                     return 1;
                }
                break;
            
            default:
                 return 1;
                break;
        }
        return 1;
    }

    /**
     * 客服信息
     * @param array $custer
     * @param array $where
     */
    public function getcuster($where='',$lang='zhtw')
    {
        $custer = Model::name($this->custer)
            ->field('*')
            ->where($where)
            ->select()
            ->toArray();
        foreach ($custer as $key => $value) {
            if(!empty($value['titles'])){
                $custer[$key]['name']=isset(json_decode($value['titles'],true)[$lang])?json_decode($value['titles'],true)[$lang]:$value['name'];
            }
            if(!empty($value['kefu_urls'])){
                $custer[$key]['kefu_url']=isset(json_decode($value['kefu_urls'],true)[$lang])?json_decode($value['kefu_urls'],true)[$lang]:$value['kefu_url'];
            }
            unset($custer[$key]['titles'],$custer[$key]['kefu_urls']);
        }
        return $custer;
    }

    /**
     * 获取基础语言列表
     * @param array $getBase 
     * @param array $where 
    */ 
    public function getLang(){ 
        $lang=[];
        $langs=[];
        $lang = Model::name($this->sysbasics)
            ->field('content,notes')
            ->where('keyname="lang"')
            ->find();
        if(!empty($lang['content'])){
            $lang['notes'] = json_decode($lang['notes'],true);
            $lang['content'] = json_decode($lang['content'],true);
            foreach ($lang['content'] as $key => $value) {
                if($key=='default'){
                   $langs[$key]= $value;
                   $langs[$value]['value']=$value;
                   $langs[$value]['name']=$lang['notes'][$key];
                   continue;
                }
                $langs[$key]['value']=$value;
                $langs[$key]['name']=$lang['notes'][$key];
            }
            unset($lang['content'],$lang['notes']);
        }else{
            $langs = config('langs');
        }
        return $langs;
    }
}